package com.cn.hotel.dao.Impl;

import com.cn.hotel.dao.ICompanyContactDao;
import com.cn.hotel.model.CompanyContactModel;
import com.cn.hotel.model.CompanyModel;
import com.cn.hotel.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CompanyContactImpl implements ICompanyContactDao {
    @Override
    public List<CompanyContactModel> findAll(CompanyContactModel companyContactModel) {
        StringBuilder sql = new StringBuilder("select * from company_contact where 1=1");
        if (!"".equals(companyContactModel.getCompanyName())){
            sql.append(" and name like '%"+companyContactModel.getCompanyName()+"%'");
        }
        sql.append(" order by id asc limit " + (companyContactModel.getPageModel().getPageNum() - 1) * companyContactModel.getPageModel().getPageSize() + "" +
                "," + companyContactModel.getPageModel().getPageSize() + "");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<CompanyContactModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                CompanyContactModel model = new CompanyContactModel();
                model.setId(resultSet.getInt("id"));
                model.setCompanyName(resultSet.getString("name"));
                model.setAdress(resultSet.getString("company_adress"));
                model.setPhone(resultSet.getString("company_phone"));
                model.setEmail(resultSet.getString("company_email"));
                model.setFax(resultSet.getString("company_fax"));
                model.setQrCode(resultSet.getString("qr_code"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通过id查询信息给编辑页面赋值
     * @param temp
     * @return
     */
    @Override
    public CompanyContactModel findById(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0) {
            sql.append("select * from company_contact  where id =" + temp);
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        CompanyContactModel model = new CompanyContactModel();
        try{
            while (resultSet.next()){
                model.setId(resultSet.getInt("id"));
                model.setCompanyName(resultSet.getString("name"));
                model.setAdress(resultSet.getString("company_adress"));
                model.setPhone(resultSet.getString("company_phone"));
                model.setEmail(resultSet.getString("company_email"));
                model.setFax(resultSet.getString("company_fax"));
                model.setQrCode(resultSet.getString("qr_code"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int edit(CompanyContactModel companyModel) {
        StringBuilder sql = new StringBuilder("update company_contact set");
        if (!"".equals(companyModel.getCompanyName())){
            sql.append(" name = '"+companyModel.getCompanyName()+"',");
        }
        if (!"".equals(companyModel.getAdress())){
            sql.append(" company_adress = '"+companyModel.getAdress()+"',");
        }
        if (!"".equals(companyModel.getPhone())){
            sql.append(" company_phone = '"+companyModel.getPhone()+"',");
        }
        if (!"".equals(companyModel.getEmail())){
            sql.append(" company_email = '"+companyModel.getEmail()+"',");
        }
        if (!"".equals(companyModel.getFax())){
            sql.append(" company_fax = '"+companyModel.getFax()+"',");
        }
        if (!"".equals(companyModel.getQrCode())){
            sql.append(" qr_code = '"+companyModel.getQrCode()+"',");
        }
        sql.append(" remark = '' where id = "+companyModel.getId()+"");
        return JDBC.executeUpdate(sql.toString());
    }

    @Override
    public int add(CompanyContactModel companyModel) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into company_contact(name,company_adress,company_phone,company_email,company_fax,qr_code) values('"+companyModel.getCompanyName()+"'," +
                "'"+companyModel.getAdress()+"','"+companyModel.getPhone()+"'," +
                "'"+companyModel.getEmail()+"','"+companyModel.getFax()+"','"+companyModel.getQrCode()+"')");
        return JDBC.executeUpdate(sql.toString());
    }

    @Override
    public int getNumber(CompanyContactModel companyContactModel) {
        StringBuilder sql = new StringBuilder("select count(*) count from company_contact where 1=1");
        if (companyContactModel.getCompanyName()!=null&&!"".equals(companyContactModel.getCompanyName())){
            sql.append(" and name like '%"+companyContactModel.getCompanyName()+"%'");
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count = 0;
        try{
            resultSet.next();
            count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int delete(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0) {
            sql.append("delete  from company_contact where id="+temp);
        }
        return JDBC.executeUpdate(sql.toString());
    }

}
